<?php
namespace Mob \ Model;

use Think \ Model;
use Think \ Hook;
/**公告模型
 * Class InfoModel
 */

require_once ('./Application/Mob/Common/function.php');

class NoticeModel extends Model {


  public function sendNotice($activityId,$title,$contents,$type = 'feed', $feed_data = array(),$createUid,$maxBrowseNum=0)
  {
   $data['activityId']=	$activityId;

   $data['noticeId']= getNoticeSerialno();
   $data['title']=$title;
   $data['content']=$contents;
   $data['createUid']=empty($createUid)?get_uid():$createUid;
   $data['maxBrowseNum']=$maxBrowseNum;
   $data['data'] = serialize($feed_data);
   $data['type']=$type;
   if($this->add($data))
   {
   return $data['noticeId'];
   }else
   {
   return null;
   }
  }

  public function releaseNotice($noticeId,$createUid)
  {
  	$where['noticeId']=$noticeId;
  	$where['createUid']=$createUid;
  	$where['status']=0;
  	$data['status']=1;
  	$data['releaseTime']=getDateTime();

    return	 $this->where($where)->save($data);

  }
  public function queryNotice($noticeId)
  {
  $where['noticeId']=$noticeId;
  $where['status']=1;
  return $this->where($where)->field('activityId,title,content,createTime,createUid,releaseTime,browseNum,maxBrowseNum,paySerialno,completeTime,supportNum,noticeType,type,data')->find();


  }

  public function browseNotice($noticeId,$browseUid)
  {
  $res = $this->isFirstBrowse($noticeId,$browseUid);
  $data['noticeId']=$noticeId;
  $data['browseUid']=$browseUid;
  $browse = M('notice_browse');
  if($res)
  {

   M('Notice')->where(" noticeId = '%s' and status = 1 ",$noticeId)->setInc('browseNum');
  $data['isFirst']=1;
  $this->where(array('noticeId'=>$noticeId))->setInc('browsenPersonNum');

  }else
  {
  $data['isFirst']=2;
  $this->where(array('noticeId'=>$noticeId))->setInc('browseNum');
  }
  $browse->add($data);
  }

  public function isFirstBrowse($noticeId,$browseUid)
  {
  $browse = M('notice_browse');
  $browseWhere['noticeId']=$noticeId;
  $browseWhere['browseUid']=$browseUid;
  $browseWhere['isFirst']=1;
  $res =$browse->where($browseWhere)->find();
  if(!$res)
  {
   return true;
  }else
  {
   return false;
  }


  }



  public function isIssue($activityId,$createUid)
  {
  $where = array('activityId'=>$activityId,'createUid'=>$createUid);
  return $this->where($where)->find();


  }
  public function getContents($NoticeId)
  {
  $where = array('noticeId'=>$NoticeId,'status'=>'1');

 return $this->where($where)->field('content,type,data,browsenPersonNum,browseNum')->find();

  }
  public function overNotice($NoticeId)
  {
  $where = array('noticeId'=>$NoticeId,'status'=>'1');
  $data['status']=3;
  $data['closeTime']=getDateTime();
  $this->where($where)->save($data);


  }













}